home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / table / tb_getauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  11.4 KB  |  605 lines

  1. /* tb_getauth.c: retrieve authorisation information */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_getauth.c,v 6.0 1991/12/18 20:24:28 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_getauth.c,v 6.0 1991/12/18 20:24:28 jpo Rel $
  9.  *
  10.  * $Log: tb_getauth.c,v $
  11.  * Revision 6.0  1991/12/18  20:24:28  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include    "head.h"
  19. #include    "util.h"
  20. #include    "chan.h"
  21. #include    "auth.h"
  22. #include    "list_rchan.h"
  23. #include    "adr.h"
  24. #include    <isode/cmd_srch.h>
  25.  
  26.  
  27.  
  28. /* -- statics -- */
  29. static Table    *Chan_auth    = NULLTBL;
  30. static Table    *Mta_auth    = NULLTBL;
  31. static Table    *User_auth    = NULLTBL;
  32.  
  33.  
  34.  
  35. /* -- command tables -- */
  36.  
  37. static  CMD_TABLE authtbl_chan[] = {
  38.     "free",            AUTH_CHAN_FREE,
  39.     "negative",        AUTH_CHAN_NEGATIVE,
  40.     "block",        AUTH_CHAN_BLOCK,
  41.     "none",            AUTH_CHAN_NONE,
  42.     "warnsender",        AUTH_CHAN_WARNS,
  43.     "warnrecipient",    AUTH_CHAN_WARNR,
  44.     "sizelimit",        AUTH_CHAN_SIZELIMIT,
  45.     "test",            AUTH_CHAN_TEST,
  46.     0,                  -1
  47.     };
  48.  
  49.  
  50.  
  51. #define AUTH_MTA_DEFAULT        1
  52. #define AUTH_MTA_REQUIRES       2
  53. #define AUTH_MTA_EXCLUDES       3
  54. #define AUTH_MTA_CONTENTEX      4
  55. #define AUTH_MTA_SIZELIMIT      5
  56.  
  57. static  CMD_TABLE authtbl_mta[] = {
  58.     "default",        AUTH_MTA_DEFAULT,
  59.     "requires",        AUTH_MTA_REQUIRES,
  60.     "excludes",        AUTH_MTA_EXCLUDES,
  61.     "content-excludes",    AUTH_MTA_CONTENTEX,
  62.     "bodypart-excludes",    AUTH_MTA_CONTENTEX,
  63.     "sizelimit",        AUTH_MTA_SIZELIMIT,
  64.     0,            -1
  65.     };
  66.  
  67.  
  68.  
  69. #define AUTH_USER_DEFAULT       1
  70. #define AUTH_USER_CONTENTEX     2
  71. #define AUTH_USER_SIZELIMIT     3
  72.  
  73. static  CMD_TABLE authtbl_user[] = {
  74.     "default",        AUTH_USER_DEFAULT,
  75.     "content-excludes",    AUTH_USER_CONTENTEX,
  76.     "bodypart-excludes",    AUTH_USER_CONTENTEX,
  77.     "sizelimit",        AUTH_USER_SIZELIMIT,
  78.     0,            -1
  79.     };
  80.  
  81.  
  82.  
  83. CMD_TABLE authtbl_rights[] = {    /* NOT static - used in auth.c */
  84.     "in",            AUTH_RIGHTS_IN,
  85.     "out",            AUTH_RIGHTS_OUT,
  86.     "both",            AUTH_RIGHTS_BOTH,
  87.     "none",            AUTH_RIGHTS_NONE,
  88.     "unset",        AUTH_RIGHTS_UNSET,
  89.     0,                  -1
  90.     };
  91.  
  92.  
  93. extern CHAN            *ch_inbound;    /* -- chan calling submit -- */
  94. extern char            *chan_auth_tbl;
  95. extern char            *mta_auth_tbl;
  96. extern char            *user_auth_tbl;
  97. extern char            *ad_getlocal();
  98. extern long            atol ();
  99.  
  100.  
  101. /* -- local routines */
  102. int                tb_getauthchan();
  103. int                tb_getauthmta();
  104. int                tb_getauthusr();
  105. void                tb_parse_authchan();
  106.  
  107. static LIST_CHAN_RIGHTS        *list_chan_rights_new();
  108. static LIST_REGEX        *list_regex_new();
  109. static int             tb_authrights();
  110. static void            add_cont_excludes();
  111. static void            add_chanrights();
  112. static void            add_regex();
  113. static void            list_chan_rights_add();
  114. static void            list_regex_add();
  115. static void            tb_getauthchan_aux();
  116. static void            tb_parse_authmta();
  117.  
  118.  
  119.  
  120.  
  121. /* --------------------- Begin  Routines ------------------------------------ */
  122.  
  123.  
  124.  
  125.  
  126. int tb_getauthchan (ochan)
  127. LIST_AUTH_CHAN        *ochan;
  128. {
  129.     char        key[MAXPATHLENGTH];
  130.  
  131.  
  132.     ochan -> li_found = FALSE;
  133.  
  134.     if (Chan_auth == NULLTBL)
  135.         if ((Chan_auth = tb_nm2struct (chan_auth_tbl)) == NULLTBL) {
  136.             PP_LOG (LLOG_EXCEPTIONS,
  137.                  ("Lib/tb_getauthchan missing %s",
  138.                 chan_auth_tbl));
  139.             return OK;
  140.         }
  141.  
  142.  
  143.     (void) sprintf (key, "%s->%s",
  144.             ch_inbound -> ch_name, ochan -> li_chan -> ch_name);
  145.     (void) tb_getauthchan_aux (key, ochan);
  146.     if (ochan -> li_found == TRUE)    return OK;
  147.  
  148.  
  149.     (void) sprintf (key, "%s->*", ch_inbound -> ch_name);
  150.     (void) tb_getauthchan_aux (key, ochan);
  151.     if (ochan -> li_found == TRUE)    return OK;
  152.  
  153.  
  154.     (void) sprintf (key, "*->%s", ochan -> li_chan -> ch_name);
  155.     (void) tb_getauthchan_aux (key, ochan);
  156.  
  157.     return OK;
  158. }
  159.  
  160.  
  161.  
  162.  
  163. void tb_parse_authchan (c, argc, argv)
  164. LIST_AUTH_CHAN        *c;
  165. int            argc;
  166. char            **argv;
  167. {
  168.     int        i, retval;
  169.     char        *p;
  170.  
  171.     for (i = 0; i < argc; i++) {
  172.         if (!isstr(argv[i]))
  173.             continue;
  174.         if ((p = index (argv[i], '=')) == NULLCP) {
  175.  
  176.             PP_DBG (("Lib/tb_parse_authchan %s", argv[i]));
  177.  
  178.             switch (retval = cmd_srch (argv[i], authtbl_chan)) {
  179.                 case AUTH_CHAN_FREE:
  180.                 c -> policy = AUTH_CHAN_FREE;
  181.                 break;
  182.                 case AUTH_CHAN_NEGATIVE:
  183.                 c -> policy = AUTH_CHAN_NEGATIVE;
  184.                 break;
  185.                 case AUTH_CHAN_BLOCK:
  186.                 c -> policy = AUTH_CHAN_BLOCK;
  187.                 break;
  188.                 case AUTH_CHAN_NONE:
  189.                 c -> policy = AUTH_CHAN_NONE;
  190.                 break;
  191.                 case AUTH_CHAN_TEST:
  192.                 c -> test   = AUTH_CHAN_TEST;
  193.                 break;
  194.                 default:
  195.                 PP_OPER (NULLCP,
  196.                      ("Lib/tb_parse_authchan error %d %s",
  197.                       retval, argv[i]));
  198.                 break;
  199.             }
  200.         }
  201.         else {
  202.             *p++ = '\0';
  203.             PP_DBG (("Lib/tb_parse_authchan %s = %s", argv[i], p));
  204.  
  205.             switch (retval = cmd_srch (argv[i], authtbl_chan)) {
  206.                 case AUTH_CHAN_WARNS:
  207.                 c -> warnsender = strdup (p);
  208.                 break;
  209.                 case AUTH_CHAN_WARNR:
  210.                 c -> warnrecipient = strdup (p);
  211.                 break;
  212.                 case AUTH_CHAN_SIZELIMIT:
  213.                 c -> sizelimit = atol (p);
  214.                 break;
  215.                 default:
  216.                 PP_OPER(NULLCP,
  217.                     ("Lib/tb_parse_authchan error %d %s %s",
  218.                      retval, argv[i], p));
  219.                 break;
  220.             }
  221.         }
  222.     }
  223. }
  224.  
  225.  
  226.  
  227.  
  228. int tb_getauthmta (omta)
  229. LIST_AUTH_MTA    *omta;
  230. {
  231.     char    *argv [MAX_AUTH_ARGS];
  232.     char    buf[BUFSIZ];
  233.     int    argc;
  234.  
  235.     PP_DBG (("Lib/tb_getauthmta (%s)", omta -> li_mta));
  236.  
  237.     omta -> li_found = FALSE;
  238.  
  239.     if (Mta_auth == NULLTBL)
  240.         if ((Mta_auth = tb_nm2struct (mta_auth_tbl)) == NULLTBL) {
  241.             PP_LOG (LLOG_EXCEPTIONS,
  242.                  ("Lib/tb_getauthmta missing %s",
  243.                  mta_auth_tbl));
  244.             return OK;
  245.         }
  246.  
  247.     if (tb_k2val (Mta_auth, omta -> li_mta, buf, TRUE) == NOTOK)
  248.         return OK;
  249.  
  250.     if ((argc = sstr2arg (buf, MAX_AUTH_ARGS, argv, " \t,")) == NOTOK)
  251.         return OK;
  252.  
  253.     omta -> li_found = TRUE;
  254.  
  255.     (void) tb_parse_authmta (omta, argc, argv);
  256.     return OK;
  257. }
  258.  
  259.  
  260.  
  261.  
  262. int tb_getauthusr (ousr, ad)
  263. AUTH_USER    *ousr;
  264. ADDR        *ad;
  265. {
  266.     char        *argv [MAX_AUTH_ARGS];
  267.     char        buf[BUFSIZ];
  268.     char        *p, *uname;
  269.     int        argc, i, rights, retval = RP_BAD, found;
  270.     CHAN        *c;
  271.  
  272.  
  273.     ousr -> found = FALSE;
  274.  
  275.     if (User_auth == NULLTBL)
  276.         if ((User_auth = tb_nm2struct (user_auth_tbl)) == NULLTBL) {
  277.             PP_LOG (LLOG_EXCEPTIONS,
  278.                  ("Lib/tb_getauthusr missing %s",
  279.                  user_auth_tbl));
  280.             return OK;
  281.         }
  282.  
  283.     found = FALSE;
  284.     
  285.     if (ad->aparse) {
  286.         if (found == FALSE
  287.             && ad->aparse->r822_str) {
  288.             if (tb_k2val (User_auth, 
  289.                       ad->aparse->r822_str,
  290.                       buf, TRUE) != NOTOK) {
  291.                 uname = ad->aparse->r822_str;
  292.                 found = TRUE;
  293.             }
  294.         }
  295.         if (found == FALSE
  296.             && ad->aparse->r822_local) {
  297.             if (tb_k2val (User_auth,
  298.                       ad->aparse->r822_local,
  299.                       buf, TRUE) != NOTOK) {
  300.                 uname = ad->aparse->r822_local;
  301.                 found = TRUE;
  302.             }
  303.         }
  304.         if (found == FALSE
  305.             && ad->aparse->x400_str) {
  306.             if (tb_k2val (User_auth,
  307.                       ad->aparse->x400_str,
  308.                       buf, TRUE) != NOTOK) {
  309.                 uname = ad->aparse->x400_str;
  310.                 found = TRUE;
  311.             }
  312.         }
  313.         if (found == FALSE
  314.             && ad->aparse->x400_local) {
  315.             if (tb_k2val (User_auth,
  316.                       ad->aparse->x400_local,
  317.                       buf, TRUE) != NOTOK) {
  318.                 uname = ad->aparse->x400_local;
  319.                 found = TRUE;
  320.             }
  321.         }
  322.     }
  323.     if (found == FALSE) {
  324.         PP_TRACE(("no authorisation entry found for user '%s'",
  325.             ad->ad_value));
  326.         return OK;
  327.     }
  328.         
  329.     PP_DBG (("Lib/tb_getauthusr (%s, '%s')", uname, buf));
  330.  
  331.  
  332.     if ((argc = sstr2arg (buf, MAX_AUTH_ARGS, argv, " \t,")) == NOTOK)
  333.         return OK;
  334.  
  335.     ousr -> found = TRUE;
  336.  
  337.  
  338.     for (i = 0; i < argc; i++) {
  339.         if (!isstr(argv[i])) 
  340.             continue;
  341.  
  342.         if ((p = index (argv[i], '=')) == NULLCP) {
  343.             PP_OPER(NULLCP,
  344.                 ("Lib/tb_getauthusr error %d %s",
  345.                  retval, argv[i]));
  346.             return NOTOK;
  347.  
  348.         }
  349.  
  350.         /* -- all of form xxx=yyy  -- */
  351.         *p++ = '\0';
  352.  
  353.         PP_DBG (("Lib/tb_getauthusr %s = %s", argv[i], p));
  354.             
  355.         switch (retval = cmd_srch (argv[i], authtbl_user)) {
  356.             case AUTH_USER_DEFAULT:
  357.             if ((rights = tb_authrights (p)) != NOTOK)
  358.                 ousr -> rights = rights;
  359.             break;
  360.             case AUTH_USER_CONTENTEX:
  361.             add_cont_excludes (&ousr -> content_excludes, 
  362.                        p);
  363.             break;
  364.             case AUTH_USER_SIZELIMIT:
  365.             ousr -> sizelimit = atol (p);
  366.             break;
  367.  
  368.             default:
  369.             if ((rights = tb_authrights (p)) != NOTOK) 
  370.                 if ((c = ch_nm2struct (argv[i])) != NULLCHAN)
  371.                     add_chanrights (&ousr -> li_cr,
  372.                             c, rights);
  373.             break;
  374.         }
  375.     }
  376.  
  377.     return OK;
  378. }
  379.  
  380.  
  381.  
  382.  
  383. /* --------------------- Static  Routines ----------------------------------- */
  384.  
  385.  
  386.  
  387.  
  388. static void tb_getauthchan_aux (str, ochan)
  389. char            *str;
  390. LIST_AUTH_CHAN        *ochan;
  391. {
  392.     int        argc;
  393.     char        *argv[MAX_AUTH_ARGS];
  394.     char        buf[BUFSIZ];
  395.  
  396.      PP_DBG (("Lib/tb_getauthchan_aux: try key %s", str));
  397.  
  398.     if (tb_k2val (Chan_auth, str, buf, TRUE) == NOTOK)
  399.         return;
  400.  
  401.     if ((argc = sstr2arg (buf, MAX_AUTH_ARGS, argv, " \t,")) == NOTOK)
  402.         return;
  403.  
  404.     ochan  -> li_found = TRUE;
  405.     (void) tb_parse_authchan (ochan, argc, argv);
  406.  
  407.     return;
  408. }
  409.  
  410.  
  411.  
  412.  
  413. static void tb_parse_authmta (omta, argc, argv)
  414. LIST_AUTH_MTA    *omta;
  415. int        argc;
  416. char        **argv;
  417. {
  418.     int    i, retval = NOTOK, rights;
  419.     char    *p;
  420.     CHAN    *c;
  421.  
  422.     for (i = 0; i < argc; i++) {
  423.         if (!isstr(argv[i]))
  424.             continue;
  425.  
  426.         if ((p = index (argv[i], '=')) == NULLCP) {
  427.             PP_OPER(NULLCP,
  428.                 ("Lib/tb_parse_authchan error %d %s",
  429.                  retval, argv[i]));
  430.             return;
  431.         }
  432.  
  433.  
  434.         /* -- all of form xxx=yyy -- */
  435.  
  436.         *p++ = '\0';
  437.  
  438.         PP_DBG (("Lib/tb_getauthmta %s = %s", argv[i], p));
  439.  
  440.         switch (retval = cmd_srch (argv[i], authtbl_mta)) {
  441.  
  442.             case AUTH_MTA_DEFAULT:
  443.             if ((rights = tb_authrights (p)) != NOTOK)
  444.                 omta -> rights = rights;
  445.             break;
  446.             case AUTH_MTA_REQUIRES:
  447.             add_regex (&omta -> requires, p);
  448.             break;
  449.             case AUTH_MTA_EXCLUDES:
  450.             add_regex (&omta -> excludes, p);
  451.             break;
  452.             case AUTH_MTA_CONTENTEX:
  453.             add_cont_excludes (&omta -> content_excludes, 
  454.                        p);
  455.             break;
  456.             case AUTH_MTA_SIZELIMIT:
  457.             omta -> sizelimit = atol (p);
  458.             break;
  459.  
  460.             default:
  461.             if ((rights = tb_authrights (p)) != NOTOK)
  462.                 if ((c = ch_nm2struct (argv[i])) != NULLCHAN) 
  463.                     add_chanrights (&omta -> li_cr,
  464.                             c, rights);
  465.             break;
  466.         }
  467.     }
  468. }
  469.  
  470.  
  471.  
  472.  
  473. static int tb_authrights (cp)
  474. char    *cp;
  475. {
  476.     int    retval;
  477.  
  478.     retval = cmd_srch (cp, authtbl_rights);
  479.  
  480.     if (retval == NOTOK)
  481.         PP_LOG (LLOG_NOTICE, ("Lib/tb_authrights not known <%s>", cp));
  482.  
  483.     PP_DBG (("Lib/tb_authrights for %s returns %d", cp, retval));
  484.     return retval;
  485. }
  486.  
  487.  
  488.  
  489.  
  490. static void add_regex (lp, str)
  491. LIST_REGEX    **lp;
  492. char        *str;    /* 1 or more regular expressions */
  493. {
  494.     char    *cp;
  495.  
  496.  
  497.     while (str) {
  498.         cp = str;
  499.         if (str = index (str, AUTH_SEPERATOR))
  500.             *str++ = '\0';
  501.         list_regex_add (lp, list_regex_new (cp));
  502.     }
  503. }
  504.  
  505.  
  506.  
  507.  
  508. static void list_regex_add (list, item)    
  509. LIST_REGEX    **list;
  510. LIST_REGEX    *item;
  511. {
  512.     LIST_REGEX    *lp = *list;
  513.  
  514.     if (lp == NULLIST_REGEX) {
  515.         *list = item;
  516.         return;
  517.     }
  518.  
  519.     while (lp -> li_next != NULLIST_REGEX)
  520.         lp = lp -> li_next;
  521.     lp -> li_next = item;
  522. }
  523.  
  524.  
  525.  
  526. static LIST_REGEX  *list_regex_new (value)    
  527. char    *value;
  528. {
  529.     LIST_REGEX    *lp;
  530.  
  531.     if (value == NULLCP)    return (NULLIST_REGEX);
  532.  
  533.     lp = (LIST_REGEX *) malloc (sizeof *lp);
  534.     bzero ((char *)lp, sizeof *lp);
  535.     lp -> li_regex = strdup (value);
  536.  
  537.     return (lp);
  538. }
  539.  
  540.  
  541.  
  542.  
  543. static void  add_cont_excludes (lp, str)
  544. LIST_BPT    **lp;
  545. char        *str;
  546. {
  547.     char    *cp;
  548.  
  549.     while (str) {
  550.         cp = str;
  551.         if ((str = index (str, AUTH_SEPERATOR)))
  552.             *str++ = '\0';
  553.         list_bpt_add (lp, list_bpt_new (cp));
  554.     }
  555. }
  556.  
  557.  
  558.  
  559.  
  560. static void add_chanrights (list, chan, rights)
  561. LIST_CHAN_RIGHTS    **list;
  562. CHAN            *chan;
  563. int            rights;
  564. {
  565.     list_chan_rights_add (list, list_chan_rights_new (chan, rights));
  566. }
  567.  
  568.  
  569.  
  570.  
  571. static void list_chan_rights_add (list, item)
  572. LIST_CHAN_RIGHTS    **list;
  573. LIST_CHAN_RIGHTS    *item;
  574. {
  575.     LIST_CHAN_RIGHTS    *lp = *list;
  576.  
  577.     if (lp == NULLIST_CHAN_RIGHTS) {
  578.         *list = item;
  579.         return;
  580.     }
  581.  
  582.     while (lp -> li_next != NULLIST_CHAN_RIGHTS)
  583.         lp = lp -> li_next;
  584.     lp -> li_next = item;
  585. }
  586.  
  587.  
  588.  
  589. static LIST_CHAN_RIGHTS    *list_chan_rights_new (chan, rights)    
  590. CHAN        *chan;
  591. int        rights;
  592. {
  593.     LIST_CHAN_RIGHTS    *lp;
  594.  
  595.     if (chan == NULLCHAN)    return (NULLIST_CHAN_RIGHTS);
  596.  
  597.     lp = (LIST_CHAN_RIGHTS *) malloc (sizeof *lp);
  598.     bzero ((char *)lp, sizeof *lp);
  599.     lp -> li_rights    = AUTH_RIGHTS_UNSET;
  600.     lp -> li_chan     = chan;
  601.     lp -> li_rights    = rights;
  602.  
  603.     return (lp);
  604. }
  605.